package me.heizi.springboot.students.controllers.apis

import me.heizi.springboot.students.controllers.dto.UserDTO
import me.heizi.springboot.students.entities.User
import me.heizi.springboot.students.model.UserType
import me.heizi.springboot.students.services.UserService
import me.heizi.springboot.students.updateNotNull
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import kotlin.IllegalArgumentException

/**
 *
 */
@RestController
@RequestMapping("/api/user")
class UserRest(
    @Autowired private val service: UserService
) {

    @get:GetMapping
    val all get() = service.all


    @DeleteMapping("{id}",)
    fun deleteOneByID(@PathVariable("id") id:Int) = service.delete(id).run {
        require(isNotEmpty()) {NullPointerException()}
        require(!contains(false)) { IllegalStateException() }
        Unit
    }




    @PutMapping("{username}")
    fun updateByName(
        @RequestBody body: UserDTO,
        @PathVariable("username") username:String
    ) {
        require(username.isNotEmpty()) {
            IllegalArgumentException("用户名为空")
        }
        require(username.isNotEmpty()) {
            IllegalArgumentException("用户名为空")
        }

    }
}